/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vms.control.weight;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;

import com.vms.Settings.ValueType;
import com.vms.control.weight.model.DateModel;
import com.vms.control.weight.model.DateModel.IOnDateChanged;
import com.vms.control.weight.model.Weight;
import com.vms.control.weight.model.WeightModel;
import com.vms.healthcontrol.WeightDataSource;

/**
 * Make sure that the main launcher activity opens up properly, which will be
 * verified by {@link #testActivityTestCaseSetUpProperly}.
 */
public class ControlWeight extends ActivityInstrumentationTestCase2<InputActivity> {

    /**
     * Creates an {@link ActivityInstrumentationTestCase2} for the {@link SkeletonActivity}
     * activity.
     */
    public ControlWeight() {
        super(InputActivity.class);
    }

    /**
     * Verifies that the activity under test can be launched.
     */
    public void testActivityTestCaseSetUpProperly() {
        assertNotNull("activity should be launched successfully", getActivity());
    }
    
    private DateModel.IOnDateChanged onDataSet = new IOnDateChanged() {
		
		@Override
		public void onDateChanged(Date date, boolean isToday) {
			assertEquals("Year must be set correct", date.getYear()+1900, 2013);
			assertEquals("Month must be set correct", date.getMonth(), 11);
			assertEquals("Day must be set correct", date.getDate(), 31);
		}
	};
    
    public void testSetDate() {
    	WeightModel model = new WeightModel(getRealContext(),ValueType.WEIGHT_MORNING);
    	
    	Calendar cl = Calendar.getInstance();
    	cl.clear();
    	cl.set(2013, 11, 31);
    	
    	model.registerDateChangeListener(onDataSet);
    	model.setDate(cl).updateDate();
        	
    }
    
    private DateModel.IOnDateChanged unregistred = new IOnDateChanged() {
		
		@Override
		public void onDateChanged(Date date, boolean isToday) {
			assertNull("This listener must be unregistred",null);
		}
	};
	
    public void testListenerRemove() {
    	WeightModel model = new WeightModel(getRealContext(),ValueType.WEIGHT_MORNING);
    	
    	model.registerDateChangeListener(unregistred);
    	model.removeDateChangeListener(unregistred);
    	model.setPrevDay().updateDate();
        	
    }
    
    /*
     * 
     */
    
    private DateModel.IOnDateChanged onDataChanged = new IOnDateChanged() {
		
		@Override
		public void onDateChanged(Date date, boolean isToday) {
			assertEquals("Year must be set correct", date.getYear()+1900, 2014);
			assertEquals("Month must be set correct", date.getMonth(), 0);
			assertEquals("Day must be set correct", date.getDate(), 1);
		}
	};
    
    public void testChangeDate() {
    	WeightModel model = new WeightModel(getRealContext(),ValueType.WEIGHT_MORNING);
    	
    	Calendar cl = Calendar.getInstance();
    	cl.clear();
    	cl.set(2013, 11, 31);
    	
    	model.setDate(cl);
    	model.registerDateChangeListener(onDataChanged);
    	model.setNextDay().updateDate();
    	model.removeDateChangeListener(onDataChanged);
    	model.registerDateChangeListener(onDataSet);
    	model.setPrevDay().updateDate();
    }
    
    private List<Weight> fillDbGenaratedData () {
    	WeightDataSource dataSource = new WeightDataSource(getRealContext());
    	WeightGenerator g = new WeightGenerator();
    	List<Weight> list = g.generate();
    	boolean addWeightList = dataSource.insertWeightList(list);
    	assertTrue("Try insert list weight fails", addWeightList);
    	return list;
    }

	private Context getRealContext() {
		return getActivity().getApplicationContext();
	}
    
    public void testInsertListWeight() {
    	cleanDatabase();
    	List<Weight> list = fillDbGenaratedData();
    	WeightDataSource dataSource = new WeightDataSource(getRealContext());
    	int allCount = dataSource.getRowsCount();
    	assertEquals(allCount, list.size());
    	
    	List<Weight> allFromDb = dataSource.getAll();
    	for (Weight weight : allFromDb) {
    		boolean contains = list.contains(weight);
			assertTrue("Not found in source list: "+weight.toString(),contains);
    	}
    }
    
    
	private void cleanDatabase() {
		WeightDataSource dbprovider = new WeightDataSource(getRealContext());
    	dbprovider.deleteAll();
	}
}
